package cn.afei.cloud.controller;

import cn.afei.cloud.config.redis.CacheUtils;
import cn.afei.cloud.feign.CloudAppFeignService;
import cn.afei.cloud.resp.ResultData;
import cn.afei.cloud.serivce.AccountService;
import cn.afei.cloud.utils.result.ResultVOUtils;
import cn.afei.cloud.utils.result.ResultVo;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @auther zzyy
 * @create 2024-01-06 16:16
 */
@RestController
@Slf4j
public class AccountController {

    @Resource
    AccountService accountService;
    @Resource
    CloudAppFeignService cloudAppFeignService;

    @GetMapping("selectPage")
    public ResultVo selectPage(HttpServletRequest request){
        CacheUtils.set("d","12342432");
        System.out.println("==============================>:" + CacheUtils.get("d"));
        return accountService.selectListPage();
    }

    /**
     * 扣减账户余额
     */
    @RequestMapping("/account/decrease")
    @SentinelResource(value = "doActionSentinelResource",
            blockHandler = "doActionBlockHandler", fallback = "doActionFallback")
    public ResultData decrease(@RequestParam("userId") Long userId, @RequestParam("money") Long money){
        accountService.decrease(userId,money);
        return ResultData.success("扣减账户余额成功！");
    }

    public String doActionBlockHandler(@PathVariable("p1") Integer p1, BlockException e){
        log.error("sentinel配置自定义限流了:{}", e);
        return "sentinel配置自定义限流了";
    }

    public String doActionFallback(@PathVariable("p1") Integer p1,Throwable e){
        log.error("程序逻辑异常了:{}", e);
        return "程序逻辑异常了"+"\t"+e.getMessage();
    }


    @GetMapping("testa")
    public String testa(HttpServletRequest request){
        return "cms->testa";
    }
     @GetMapping("testb")
    public ResultVo testb(HttpServletRequest request){
         String testa = cloudAppFeignService.testa();
         String dddd= "cms->testb" + "--------------" + testa;
         return ResultVOUtils.success(dddd);
    }



    @GetMapping("/cms/testa")
    public ResultVo testa2(HttpServletRequest request){
        return ResultVOUtils.success("cms->testa");
    }
}
